package com.jl.jlapp.mvp.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.jl.jlapp.R;
import com.jl.jlapp.eneity.PostModel;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.nets.Transformer;
import com.jl.jlapp.popu.ClearHistorySearhPopu;
import com.jl.jlapp.utils.ToastUtils;
import com.jl.jlapp.utils.Tools;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserNameSettingActivity extends Activity {
    ClearHistorySearhPopu clearHistorySearhPopu;
    String name = "";
    SharedPreferences sharedPreferences;
    int userId = 0;
    @BindView(R.id.user_name)
    EditText userNameET;

    private void updateUserName(final String paramString) {
        Net.get().updateUserName(this.userId, paramString)
                .compose(Transformer.<PostModel>switchSchedulers())
                .subscribe(new CommonObserver<PostModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(PostModel postModel) {
                        if (postModel.getCode()==200){
                            ToastUtils.show("设置用户名成功！");
                            sharedPreferences = getSharedPreferences("JLAppData", 0);
                            sharedPreferences.edit().putString("userName", paramString).commit();
                            finish();
                        }else{
                            ToastUtils.show("设置用户名失败！");
                        }
                    }
                });
    }

    @OnClick({R.id.ok_btn, R.id.input_del_img, R.id.icon_return})
    public void onClick(View paramView) {
        String str = this.userNameET.getText().toString().trim();
        switch (paramView.getId()) {
            case R.id.ok_btn:
                if ("".equals(str)) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (str.length() < 2) {
                    Toast.makeText(this, "请输入2-20个字符", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!str.matches("^[一-龥a-zA-Z]+[一-龥\\w-]+")) {
                    Toast.makeText(this, "用户名不合法，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                updateUserName(str);
                break;
            case R.id.input_del_img:
                this.userNameET.setText("");
                break;
            case R.id.icon_return:
                finish();
                break;
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_user_name_setting);
        ButterKnife.bind(this);
        Tools.addActivity(this);
        this.sharedPreferences = getSharedPreferences("JLAppData", 0);
        this.userId = this.sharedPreferences.getInt("userId", -1);
        this.name = this.sharedPreferences.getString("userName", "");
        this.userNameET.setText(this.name);
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
            String trim = this.userNameET.getText().toString().trim();
            if ((!"".equals(trim)) && (!this.name.equals(trim))) {
                this.clearHistorySearhPopu = new ClearHistorySearhPopu(this, new OnClickListener() {
                    public void onClick(View paramAnonymousView) {
                        UserNameSettingActivity.this.clearHistorySearhPopu.dismiss();
                        UserNameSettingActivity.this.finish();
                    }
                }, Integer.valueOf(9));
                this.clearHistorySearhPopu.showAtLocation(findViewById(R.id.user_name_setting_page), 17, 0, 0);
            } else {
                finish();
            }
        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/UserNameSettingActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */